/**
 * X号码表
 * @date 2025-08-20
 * @author exchenming
 * @Email 276490943@qq.com
 */
import { getAllProvince, getCityByProvince } from '@/api/x/noQs'
import { FormSchema } from '@/components/Form'
import { BasicColumn } from '@/components/Table'

export const tableColumns: BasicColumn[] = [
  { dataIndex: 'xno', title: 'X号码', width: 80, },
  { dataIndex: 'openTime', title: '开通时间', width: 120 },

  { dataIndex: 'provinceName', title: '省份名称', width: 80 },
  //{ dataIndex: 'provinceCode', title: '省份CODE', width: 80 },
  { dataIndex: 'cityName', title: '城市名称', width: 80 },
  //{ dataIndex: 'cityCode', title: '城市CODE', width: 80 },
  { dataIndex: 'isUse', title: '是否使用', width: 40 },
  { dataIndex: 'isLock', title: '是否锁定', width: 40 },
  { dataIndex: 'isCancel', title: '是否退订', width: 40 },
  { dataIndex: 'cancelTime', title: '退订日期', width: 120 },
]

export const searchFormSchema: FormSchema[] = [
  { 
  field: 'openTime', 
  label: '开通时间', 
  component: 'RangePicker',
  },
{
    field: 'provinceName',
    label: '省份',
    component: 'ApiSelect',
    defaultValue: null,
    componentProps: {
      api: getAllProvince,
      resultField: 'data',
      labelField: 'provinceName',
      valueField: 'provinceName',
      allowClear: true,
      immediate: true,
      onChange: (val, option, formModel) => {
        if (!formModel) return
        // 切换省份时清空城市
        formModel.cityName = null
      }
    }
  },
  {
    field: 'cityName',
    label: '城市',
    component: 'ApiSelect',
    defaultValue: null,
    componentProps: ({ formModel }) => {
  const province = formModel.provinceName
  return {
    key: formModel.provinceName || 'city', // 🔑 根据省份动态刷新组件
    api: () => {
      formModel.cityName = null  // 切换省份时，清空城市
      if (!province) return Promise.resolve([])  // 防止 province null
      return getCityByProvince({ provinceName: province })
    },
    resultField: 'data',
    labelField: 'cityName',
    valueField: 'cityName',
    allowClear: true,
    immediate: true
  }
}
  },
  {
    field: 'xno',
    label: 'X号码',
    component: 'Input',
    defaultValue: null,
    componentProps: { allowClear: true }
  },

  {
    field: 'isLock',
    label: '锁定',
    component: 'Select',
    defaultValue: null,
    componentProps: { allowClear: true, options: [{ label: '已锁定', value: 'Y' }, { label: '未锁定', value: 'N' }] }
  },
  {
    field: 'isCancel',
    label: '退订',
    component: 'Select',
    defaultValue: null,
    componentProps: { allowClear: true, options: [{ label: '已退订', value: 'Y' }, { label: '未退订', value: 'N' }] }
  },
  {
    field: 'isUse',
    label: '是否使用',
    component: 'Select',
    defaultValue: null,
    componentProps: { allowClear: true, options: [ { label: '已使用', value: 'Y' }, { label: '未使用', value: 'N' }] }
  }
]

export const editFormSchema: FormSchema[] = [
  { field: 'id', label: '唯一标识', component: 'InputNumber', show: false },
  { field: 'xno', label: 'X号码', component: 'InputNumber' },
  { field: 'provinceName', label: '省份名称', component: 'Input' },
  //{ field: 'provinceCode', label: '省份CODE', component: 'Input' },
  { field: 'cityName', label: '城市名称', component: 'Input' },
  //{ field: 'cityCode', label: '城市CODE', component: 'Input' },
  { field: 'openTime', label: '开通时间', component: 'Input' },
  { field: 'isUse', label: '是否使用', component: 'Select', componentProps: { allowClear: true, options: [{ label: '已使用', value: 'Y' }, { label: '未使用', value: 'N' }] } },
  { field: 'isLock', label: '是否锁定', component: 'Select', componentProps: { allowClear: true, options: [{ label: '已锁定', value: 'Y' }, { label: '未锁定', value: 'N' }] } },
  {
    field: 'isCancel', label: '是否退订', component: 'Select', componentProps: { allowClear: true, options: [{ label: '已退订', value: 'Y' }, { label: '未退订', value: 'N' }] }
  },
  { field: 'cancelTime', label: '退订日期', component: 'Input' },
]